package com.team.dao.impl;




import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.team.dao.UserDao;
import com.team.entity.Admin;
import com.team.entity.User;

@Repository
public class UserDaoImpl implements UserDao {
	@Autowired
	private SessionFactory sessionFactory;

	public void saveuser(User user) {
		this.sessionFactory.getCurrentSession().save(user);
	}
	@Override
	public User seleu(String username) {
		User user=null;
		String hql = "from User where username=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, username);
			user= (User) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}
	@Override
	public User seleuser(String username, String password) {
		User user=null;
		String hql = "from User where username=? and password=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, username);
			query.setParameter(1, password);
			user= (User) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public Admin seleadmin(String username, String password) {
		Admin admin= null;
		String hql = "from Admin where username=? and password=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, username);
			query.setParameter(1, password);
			admin= (Admin) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return admin;
	}

	@Override
	public Admin selea(int id) {
		Admin admin= null;
		String hql = "from Admin where id=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, id);
			admin= (Admin) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return admin;
		
	}
	@Override
	public boolean upadmin(Admin admin) {
		this.sessionFactory.getCurrentSession().update(admin);
		return true;
	}
	@Override
	public boolean upuser(User user) {
		this.sessionFactory.getCurrentSession().update(user);
		return true;
	}
	@Override
	public List<User> alluser() {
		String hql="from User";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		List<User> list=query.list();
		return list;
	}
	@Override
	public boolean deleuser(int id) {
		String hql="from User where id=?";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, id);
		User user = (User) query.uniqueResult();
		this.sessionFactory.getCurrentSession().delete(user);
		return true;
	}
	@Override
	public User selepassword(String username, String no, String name, String department) {
		User user= null;
		String hql = "from User where username=? and no=? and name=? and department=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, username);
			query.setParameter(1, no);
			query.setParameter(2, name);
			query.setParameter(3, department);
			user= (User) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}
	
}
